import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      redirect: '/search',
      children: [
        {
          path: 'search',
          name: 'search',
          // route level code-splitting
          // this generates a separate chunk (search.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/SearchView.vue')
        },
        {
          path: 'deliver',
          name: 'deliver',
          // route level code-splitting
          // this generates a separate chunk (deliver.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/DeliverView.vue')
        },
        {
          path: 'userinfo',
          name: 'userinfo',
          // route level code-splitting
          // this generates a separate chunk (userinfo.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/UserInfoView.vue')
        },
        {
          path: 'detail',
          name: 'detail',
          // route level code-splitting
          // this generates a separate chunk (detail.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/SearchDetailView.vue')
        },
        {
          path: 'mytrip',
          name: 'mytrip',
          // route level code-splitting
          // this generates a separate chunk (mytrip.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/MytripView.vue'),
        },
        {
          path: 'historytrip',
          name: 'historytrip',
          // route level code-splitting
          // this generates a separate chunk (historytrip.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/HistorytripView.vue')
        },
        {
          path: 'tripdetail',
          name: 'TripDetail',
          // route level code-splitting
          // this generates a separate chunk (mytrip.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/TripDetailView.vue')
        },
        {
          path: 'finishedtrip',
          name: 'FinishedTripDetail',
          // route level code-splitting
          // this generates a separate chunk (historytrip.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/FinishedTripDetailView.vue')
        },
        {
          path: 'updateuser',
          name: 'updateuser',
          // route level code-splitting
          // this generates a separate chunk (updateuser.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/UpdateuserView.vue')
        },
        {
          path: 'action',
          name: 'action',
          // route level code-splitting
          // this generates a separate chunk (action.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/ActionView.vue'),
        },
        {
          path: 'about',
          name: 'about',
          // route level code-splitting
          // this generates a separate chunk (about.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/AboutView.vue')
        },
        {
          path: 'tripinformation',
          name: 'tripinformation',
          // route level code-splitting
          // this generates a separate chunk (tripinformation.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/TripDetailView.vue'),
        },
        {
          path: 'historyinformation',
          name: 'historyinformation',
          // route level code-splitting
          // this generates a separate chunk (historyinformation.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/FinishedTripDetailView.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') // 使用完整路径
    return next();

  //获取token
  const tokenStr = window.localStorage.getItem('token');
  if (!tokenStr) {
    alert('您还没有登录，请先登录');
    return next('/login'); // 使用完整路径
  } else {
    next();
  }
})


export default router
